<template>
  <div class="app-container">
    <div class="filter-container">
      <movie-select v-model="filters.movieId" />
      <org-unit-select v-model="filters.orgUnitId" placeholder="请选择店铺" :org-unit-type="1" />
      <room-select v-model="filters.roomId" placeholder="请选择房间" :org-unit-id="filters.orgUnitId" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="handleAdd">添加内测剧本</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column type="index" width="60" />
      <el-table-column prop="movieName" label="剧本名称" min-width="180" />
      <el-table-column prop="orgUnitName" label="店铺" min-width="240" />
      <el-table-column prop="roomName" label="房间" min-width="240" />
      <el-table-column label="操作" min-width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="danger" size="small" @click="handleDelete(scope.index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="filters.page" :limit.sync="filters.limit" @pagination="loadPageList" />
    <!--新增界面-->
    <el-dialog title="剧本内测配置" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" :label-width="formLabelWidth" :rules="addFormRules">
        <el-form-item label="剧本" prop="name">
          <movie-select v-model="addForm.movieId"/>
        </el-form-item>
        <el-form-item label="店铺" prop="price">
          <org-unit-select v-model="addForm.orgUnitId" placeholder="请选择店铺" :org-unit-type="1" />
        </el-form-item>
        <el-form-item label="房间" prop="price">
          <room-select v-model="addForm.roomId" placeholder="请选择房间" :org-unit-id="addForm.orgUnitId"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="addLoading" @click="addSubmit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import OrgUnitSelect from '../components/org-unit-select'
import RoomSelect from '../components/room-select'
import MovieSelect from '../components/movie-select'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { mapGetters } from 'vuex'
import { deleteMovieTest, saveMovieTest, listMovieTests } from '@/api/inner-test'

export default {
  name: 'MovieInnerTest',
  directives: { waves },
  components: { Pagination, OrgUnitSelect, RoomSelect, MovieSelect },
  data() {
    return {
      tableHeight: 260,
      list: null,
      total: 0,
      listLoading: true,
      filters: {
        page: 1,
        limit: 20,
        orgUnitId: null,
        roomId: null,
        movieId: null
      },
      // 新增界面是否显示
      addFormVisible: false,
      addLoading: false,
      addFormRules: {
        movieId: [{ required: true, message: '请选择剧本', trigger: 'blur' }],
        roomId: [{ required: true, message: '请选择房间', trigger: 'blur' }]
      },
      // 新增界面数据
      addForm: {
        movieId: null,
        orgUnitId: null,
        roomId: null
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'formLabelWidth'])
  },
  created() {
    this.tableHeight = window.innerHeight - 200
    this.loadPageList()
  },
  methods: {
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters }
      listMovieTests(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          const { records, total } = data || {}
          this.list = records
          this.total = total
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.loadPageList()
    },
    sortChange({ prop, order }) {
      this.handleFilter()
    },
    // 显示新增界面
    handleAdd: function() {
      this.addFormVisible = true
      this.addForm.movieId = null
      this.addForm.orgUnitId = null
      this.addForm.roomId = null
      this.$nextTick(() => {
        this.$refs['addForm'].clearValidate()
      })
    },
    // 新增
    addSubmit: function() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true
            this.doAdd()
          })
        }
      })
    },
    doAdd() {
      const param = { ...this.addForm }
      saveMovieTest(param).then(({ success, message }) => {
        this.addLoading = false
        if (success) {
          this.$message({
            message: message || '提交成功',
            type: 'success'
          })
          this.$refs['addForm'].resetFields()
          this.addFormVisible = false
          this.loadPageList()
        } else {
          this.$message({
            message: message || '保存失败',
            type: 'error'
          })
        }
      }, () => {
        this.addLoading = false
      })
    },
    handleDelete(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        deleteMovieTest(row.id).then(({ success, message }) => {
          this.listLoading = false
          if (success) {
            this.$message({
              message: message || '删除成功',
              type: 'success'
            })
            this.loadPageList()
          } else {
            this.$message({
              message: message || '删除失败',
              type: 'error'
            })
          }
        }, (res) => {
          this.listLoading = false
        })
      }).catch(() => {

      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
